﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;


public partial class Employees_EmployeeTreeView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Form["dir"] != null)
            {
                string mode = "view";
                if (Request.Form["mode"] != null)
                {
                    mode = Server.UrlDecode(Request.Form["mode"].ToString());
                }
                string dir;

                MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
                master.Visible = false;

                dir = Server.UrlDecode(Request.Form["dir"]);
                //System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
                if (dir == "-1")
                {
                    Response.Write("<ul class=\"jqueryFileTree\" style=\"display: none;\">\n");
                    Response.Write("<li class=\"directory collapsed\"><a href=\"#\" rel=\"0/\"  mode=\"" + mode + "\">Hyperion</a></li>\n");
                    Response.Write("</ul>");
                }
                else
                {
                    dir = dir.Substring(0, dir.Length - 1);

                    GetRouter(Convert.ToInt32(dir), mode);
                }
            }
            else if (Request["ItemParam"] != null)
            {
               
                MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
                master.HelpHref = "";

                master.PicUrl = "~/image/PageIcon/shu_emp.jpg";

                if (Request["ItemParam"].ToString() == "emp")
                {
                    //DepartmentTreeView.Bind(DepartmentTreeView.ShowDptAndJobAndEmp);

                    master.PageName = "组织架构表";

                    AddScript();
                }
                else
                {
                    //DepartmentTreeView.Bind(DepartmentTreeView.DptAndEmp);
                    if (Request["ItemParam"].IndexOf("view") != -1)
                    {

                        master.PageName = "组织架构表(部门信息)";


                        AddScript();
                    }
                    else
                    {


                        master.PageName = "组织架构表(人事维护)";
                        AddScript();
                    }
                }
                master.IntiBar();

            }



        }
    }

    private void AddScript()
    {
        string iparar = Request["ItemParam"];
        string href = "EmployeeDetail.aspx?do=false";
        string action = "view";
        switch (iparar)
        {
            case "emp":
                href = "#";
                break;
            default:
                string[] router = iparar.Split('?');
                href += "&action=" + router[0] + "&mode=" + router[1];
                action = router[0];
                if (action == "view")
                {
                    object DeptId = Srvtools.CliUtils.CallMethod("SDepartment", "GetDptIdByEmpId", new object[] { Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]) })[1];
                    if (DeptId != null)
                    {
                        DataTable dt = ((DataSet)DeptId).Tables[0];
                        action = "";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            action += dt.Rows[i][0].ToString() + ",";
                        }

                        if (action.Length <= 0)
                        {
                            action = "dpt?-1";
                        }
                        else
                        {
                            action = "dpt?" + action.Substring(0,action.Length - 1);
                        }
                    }
                    else
                    {
                        action = "dpt?-1";
                    }
                }
                break;


        }

        System.Web.UI.HtmlControls.HtmlGenericControl jquery = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
        jquery.Attributes["type"] = "text/javascript";
        jquery.Attributes["src"] = "../JQuery/jquery-1.3.2.min.js";


        System.Web.UI.HtmlControls.HtmlGenericControl jqueryFileTree = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
        jqueryFileTree.Attributes["type"] = "text/javascript";
        jqueryFileTree.Attributes["src"] = "../js/TreeView/jqueryFileTree.js";

        System.Web.UI.HtmlControls.HtmlGenericControl easing = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
        easing.Attributes["type"] = "text/javascript";
        easing.Attributes["src"] = "../js/TreeView/jquery.easing.js";


        System.Web.UI.HtmlControls.HtmlLink link = new System.Web.UI.HtmlControls.HtmlLink();
        link.Attributes.Add("type", "text/css");
        link.Attributes.Add("rel", "stylesheet");
        link.Attributes.Add("href", "../js/TreeView/jqueryFileTree.css");

        System.Web.UI.HtmlControls.HtmlGenericControl ready = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
        ready.Attributes["type"] = "text/javascript";
        ready.InnerHtml = "$(document).ready(function() {"
                            + "$('#container_id').fileTree({"
                            + "root: '-1',"
                             + "mode: '" + action + "',"
                            + "script: 'EmployeeTreeView.aspx',"
                            + "folderEvent: 'click', expandSpeed: 750, collapseSpeed: 750, expandEasing: 'easeOutBounce', collapseEasing: 'easeOutBounce', loadMessage: 'Un momento...'"
                            + " }, function(file) {"
                            + "location.href='" + href + "&eid='+file"
                            + "});});";


        this.Header.Controls.Add(jquery);
        this.Header.Controls.Add(jqueryFileTree);
        this.Header.Controls.Add(easing);
        this.Header.Controls.Add(ready);
        this.Header.Controls.Add(link);


    }

    private void GetRouter(int id, string mode)
    {

        object[] ret = null;
        
        if (Server.UrlDecode(mode).IndexOf("dpt?") != -1)
        {
            string[] dpt = Server.UrlDecode(mode).Split('?');
            string[] dptid = dpt[1].Split(',');
            string wdpt = "";
            for (int i = 0; i < dptid.Length; i++)
            {
                wdpt += "departmentid like '" + dptid[i] + "%' or ";
            }
            if (wdpt.Length <= 0)
            {
                wdpt = "(departmentid like '-1%')";
            }
            else
            {
                wdpt = "(" + wdpt.Substring(0, wdpt.Length - 4) + ")";
            }
            ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetEmpJobNameByDid", new object[] { id, wdpt });
        }
        else
        {
            ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetEmpJobNameByDid", new object[] { id, "1=1"});
        }
        if (ret[1] != null)
        {
            Response.Write("<ul class=\"jqueryFileTree\" style=\"display: none;\">\n");
            DataTable dt = ((DataSet)ret[1]).Tables[0];
            if (dt.Rows.Count > 0)
            {
                GetEmpAndJob(dt.Select("mode='e'"));
                GetDpt(dt.Select("mode='d'"), mode);
            }
            Response.Write("</ul>");
        }
    }

    private void GetEmpAndJob(DataRow[] dr)
    {
        for (int i = 0; i < dr.Length; i++)
        {
            if (dr[i]["rightid"].ToString() == "1")
            {
                Response.Write("\t<li class=\"file ext_male\"><a href=\"#\" rel=\"" + dr[i]["id"].ToString() + "\">" + dr[i]["rightname"].ToString() + "————————————————————" + dr[i]["name"].ToString() + "</a></li>\n");
            }
            else
            {
                Response.Write("\t<li class=\"file ext_female\"><a href=\"#\" rel=\"" + dr[i]["id"].ToString() + "\">" + dr[i]["rightname"].ToString() + "————————————————————" + dr[i]["name"].ToString() + "</a></li>\n");
            }
        }
    }

    private void GetDpt(DataRow[] dr, string mode)
    {
        for (int i = 0; i < dr.Length; i++)
        {

            if (dr[i]["rightname"] != null && dr[i]["rightname"].ToString() != "")
            {
                Response.Write("<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + dr[i]["id"].ToString() + "/\"  mode=\"" + mode + "\"><span class='dpt'>" + dr[i]["name"].ToString() + "————————————————————" + dr[i]["rightname"].ToString() + "（负责人）</span></a></li>\n");
            }
            else
            {
                Response.Write("<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + dr[i]["id"].ToString() + "/\"  mode=\"" + mode + "\"><span class='dpt'>" + dr[i]["name"].ToString() + "</span></a></li>\n");
            }

            //Response.Write("\t<li class=\"file ext\"><a href=\"#\" rel=\"" + dr[i]["id"].ToString() + "\">" + dr[i]["rightname"].ToString() + "-------" + dr[i]["name"].ToString() + "</a></li>\n");
        }
    }
}
